package edu.stanford.prpl.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * StreamUtils
 *
 * @author brigham (on Apr 29, 2008 at 11:50:31 AM)
 */
public class StreamUtils {

    private static int DEFAULT_BUFFER_SIZE = 8192;

    /**
     * Copy an input stream to an output stream using the default buffer size.
     *
     * @param in  inputstream from which to read
     * @param out output stream to which to write
     * @throws IOException if some I/O error occurs
     */
    public static void copyStream(InputStream in, OutputStream out) throws IOException {
        copyStream(in, out, new byte[DEFAULT_BUFFER_SIZE]);
    }

    /**
     * Copy an input stream to an output stream using the specfied buffer.
     *
     * @param in  inputstream from which to read
     * @param out output stream to which to write
     * @param buffer buffer to use for copying bytes
     * @throws IOException if some I/O error occurs
     */
    public static void copyStream(InputStream in, OutputStream out, byte[] buffer) throws IOException {
        int bytesRead;
        while ((bytesRead = in.read(buffer)) > 0) {
            out.write(buffer, 0, bytesRead);
        }
    }
}
